package com.haoshuang.zhouzhoubang.ui.adapter

import com.haoshuang.zhouzhoubang.R
import com.haoshuang.zhouzhoubang.databinding.ItemAppointmentOrderBinding
import com.haoshuang.zhouzhoubang.model.bean.OrderBean
import com.haoshuang.zhouzhoubang.ui.dialog.ShowRewardNoticeDialog
import com.lxj.xpopup.XPopup
import com.mufeng.libs.base.BaseBindingHolder
import com.mufeng.libs.base.BaseBindingQuickAdapter
import com.mufeng.libs.utils.view.clickWithTrigger
import com.mufeng.libs.utils.view.gone
import com.mufeng.libs.utils.view.horizontal
import com.mufeng.libs.utils.view.load
import com.mufeng.libs.utils.view.visible
import com.mufeng.libs.utils.view.visibleOrGone


/**
 * 预约订单适配器
 */
class AppointmentOrderAdapter(data: MutableList<OrderBean>) : BaseBindingQuickAdapter<OrderBean, ItemAppointmentOrderBinding>(data) {

    override fun onBindViewHolder(holder: BaseBindingHolder, position: Int, item: OrderBean?) {
        if (item == null) return
        holder.getViewBinding<ItemAppointmentOrderBinding>().apply {
            // 重点区域
            tvKeyArea.text = "重点区域: ${item.key_areas}"
            tvKeyArea.visibleOrGone(!item.key_areas.isNullOrEmpty())

            val adapter = OrderTeamMemberAdapter(item.team_member ?: mutableListOf())
            recyclerView.horizontal().adapter = adapter
            llTeamMember.visibleOrGone(item.team_member?.isNotEmpty() == true)
            ivPlatformOrder.visibleOrGone(item.is_platform_order == 1)
//            val nighttime_amount = item.nighttime_amount?.toDoubleOrNull()?:0.0
//            tvNighttimeAmount.visibleOrGone(nighttime_amount > 0.0)
            llRewardView.clickWithTrigger {
                XPopup.Builder(context)
                    .asCustom(ShowRewardNoticeDialog(context))
                    .show()
            }
            llRewardView.visibleOrGone(item.reward_amount != null && 0.0 != item.reward_amount?.toDoubleOrNull())
            tvRewardMoney.text = "${item.reward_amount}元"
            tvUserName.text = item.real_name
            if (item.user_phone.length == 11) {
                tvPhone.text = item.user_phone.replaceRange(4,7, "****")
            } else {
                tvPhone.text = item.user_phone
            }
            tvTime.text="剩余次数${item.residue_num}"
            tvServiceTime.text = "有效时间：" + item.meal_end_time
            tvStoreName.text = "服务内容：" + item.store_name
            tvAddress.text = item.base_address
            tvRemark.text = item.mark
            payPrice.text = "¥" + item.pay_price
            ivHead.load(item.user_avatar, R.mipmap.rl)
            if (item.show_status=="1"){
                ivOrderType.setImageResource(R.mipmap.taoc)
            }else if (item.show_status=="2"){
                ivOrderType.setImageResource(R.mipmap.tddd09)
            }else{
                ivOrderType.setImageResource(R.mipmap.grdd09)
            }
            when(item.user_status){
                "1"->{
                    ivOrderRole.visible()
                    ivOrderRole.setImageResource(R.mipmap.duizhang)
                }
                "2"->{
                    ivOrderRole.visible()
                    ivOrderRole.setImageResource(R.mipmap.fzr)
                }
                "3"->{
                    ivOrderRole.visible()
                    ivOrderRole.setImageResource(R.mipmap.dyy)
                }
                else->{
                    ivOrderRole.gone()
                }
            }

            tvTotalNum.text = "数量 x${item?.total_num.toString()}"

        }
    }
}
